package ecustom.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

import ecustom.common.util.CustomUtil;
import weaver.hrm.User;

public class AbstractWebAction {
	
	protected Logger log = Logger.getLogger(this.getClass());
	public static final int RESP_OK = 200;
	public static final int RESP_ERROR = 500;
	
	protected JSONObject ok() {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_OK);
		resp.put("message", "Success");
		return resp;
	}
	
	public JSONObject ok(Object data) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_OK);
		resp.put("message", "Success");
		resp.put("data", data);
		return resp;
	}
	
	public JSONObject ok(Object data, String message) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_OK);
		resp.put("message", message);
		resp.put("data", data);
		return resp;
	}
	
	/**
	 * 
	 * @param requestid
	 * @param message
	 * @return
	 */
	public JSONObject okReq(Object requestid, String message) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_OK);
		resp.put("message", message);
		resp.put("requestid", requestid);
		return resp;
	}
	
	/**
	 * 
	 * @param requestid
	 * @param message
	 * @return
	 */
	public JSONObject errReq(Object requestid, String message) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_ERROR);
		resp.put("message", message);
		resp.put("requestid", requestid);
		return resp;
	}
	
	public JSONObject err(String message, String data) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_ERROR);
		resp.put("message", message);
		resp.put("data", data);
		return resp;
	}
	
	public JSONObject err(String message) {
		JSONObject resp = new JSONObject();
		resp.put("code", RESP_ERROR);
		resp.put("message", message);
		resp.put("data", "");
		return resp;
	}
	
	protected String getClientIp(HttpServletRequest req) {
		String ip = req.getHeader("x-forwarded-for");
		if (ip != null && !ip.isEmpty() && !"unknown".equals(ip))
			return ip;
		
		ip = req.getHeader("X-Real-IP");
		if (ip != null && !ip.isEmpty() && !"unknown".equals(ip))
			return ip;
		
		ip = req.getHeader("Proxy-Client-IP");
		if (ip != null && !ip.isEmpty() && !"unknown".equals(ip))
			return ip;
		
		ip = req.getHeader("WL-Proxy-Client-IP");
		if (ip != null && !ip.isEmpty() && !"unknown".equals(ip))
			return ip;
		
		return req.getRemoteAddr();
	}
	
	protected String getBody(HttpServletRequest req) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
		String line = null;
		StringBuilder body = new StringBuilder();
		while ((line = br.readLine()) != null) {
			body.append(line);
		}
		if (log.isDebugEnabled()) log.debug("getBody() body: " + body);
		return body.toString();
	}
	
	protected Integer getInteger(HttpServletRequest req, String name) throws IOException {
		String value = req.getParameter(name);
		return CustomUtil.getInteger(value);
	}
	
	protected Integer getIntegerCK(HttpServletRequest req, String name) throws IOException {
		String value = getStringCK(req, name);
		return CustomUtil.getIntCK(value, "参数“" + name + "”不是整数，value: " + value);
	}
	
	protected String getString(HttpServletRequest req, String name) throws IOException {
		String value = req.getParameter(name);
		return value;
	}
	
	protected String getStringCK(HttpServletRequest req, String name) throws IOException {
		String value = getString(req, name);
		if (value == null || value.isEmpty())
			throw new RuntimeException("缺少请求参数“" + name + "”或参数值为空！");
		return value;
	}
	
	protected String getUrl(HttpServletRequest req) {
		String url = req.getServletPath() + req.getPathInfo();
		if (req.getQueryString() != null && !req.getQueryString().isEmpty())
			url += "?" + req.getQueryString();
		return url;
	}
	
	protected User getUserCK(HttpServletRequest req) throws IOException {
		User user = (User)req.getSession(true).getAttribute("weaver_user@bean");
		if (user == null)
			throw new RuntimeException("登录超时，请重新登录！");
		return user;
	}
}
